﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControlsLib;
using GestionePolveriLib.BL;
using GestionePolveriLib.BL.Filters;
using GestionePolveri2.Filters;
using GestionePolveriLib.Facades;
using FrameworkLib.Filters;
using GestionePolveri2.DAL;

namespace GestionePolveri2
{
    public partial class AnagraficaFatture : AnagraficaFattureBaseExt
    {
        public AnagraficaFatture()
        {
            InitializeComponent();
        }

        public override void InitializeForm()
        {
            base.InitializeForm();

            this.CanModify = false;
            this.IsFiltering = true;
            this.Filter.FilterBag.UseDates = false;
            this.ShowOnlyCustomButton = true;
            this.InitializeButtonBar();

            this.ItemSelected += new EventHandler<CustomEventArgs<Fattura>>(Anagrafica_ItemSelected);

            ManageData();
        }

        public override void OnRefreshFilterData()
        {
            base.OnRefreshFilterData();

            this.objectListView1.Unsort();

            this.objectListView1.ShowGroups = true;

            ManageData();
        }

        public override void OnFilterExecuted()
        {
            base.OnFilterExecuted();

            this.objectListView1.ShowGroups = true;

            ManageData();
        }

        public override void OnFilterReset()
        {
            this.objectListView1.Unsort();

            this.objectListView1.ShowGroups = true;

            base.OnFilterReset();

            ManageData();
        }

        private void ManageData()
        {
            olvFattureDetails.ClearObjects();
            objectListView1.ClearObjects();
            LoadData(this.Filter.FilterBag);
        }

        private void LoadData(FilterBase<Fatture> filter)
        {
            FattureFacade ff = new FattureFacade();
            ff.RunAsync<List<Fattura>>(() => ff.Get(filter), (result) =>
            {
                if (result != null)
                {
                    this.objectListView1.SetObjects((List<Fattura>)result, true);
                }
            });
        }

        void Anagrafica_ItemSelected(object sender, CustomEventArgs<Fattura> e)
        {
            base.Current = e.Item;

            LottoFacade lf = new LottoFacade();
            List<Lotto> m_lotti = lf.GetLottiByFattura(base.Current.ID);

            this.olvFattureDetails.SetObjects((List<Lotto>)m_lotti, true);
        }
    }

    public class AnagraficaFattureBaseExt : AnagraficaManagerFilteredObjectListView<Fattura, AnagraficaFattureFilter, AnagraficaFattureFilterBag> { }
}
